﻿using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;

namespace CoreReset
{
    public static class MRU
    {
        internal static event MRUEventHandler ItemClicked;

        private static Collection<string> items;
        public static Collection<string> Items
        {
            get { return items; }
        }

        public static void Load()
        {
            items = new Collection<string>();
            RegistryKey appKey = Application.UserAppDataRegistry.OpenSubKey("MRU");
            if (appKey != null)
            {
                String[] values = appKey.GetValueNames();
                foreach (String value in values)
                {
                    Items.Add(appKey.GetValue(value).ToString());
                }
                appKey.Close();
            }
        }

        public static void Save()
        {
            Application.UserAppDataRegistry.DeleteSubKey("MRU", false);
            RegistryKey appKey = Application.UserAppDataRegistry.CreateSubKey("MRU");
            int i = 0;
            foreach (String mruItem in Items)
            {
                appKey.SetValue(i.ToString(), mruItem);
                i++;
            }
            appKey.Close();
        }

        public static void Insert(String fileName)
        {
            if (Items.Contains(fileName))
            {
                Items.Remove(fileName);
                Items.Insert(0, fileName);
            }
            else
            {
                Items.Insert(0, fileName);
            }
            Save();
        }

        private static void UpdateMRUNumbers(ToolStripMenuItem menuItem)
        {
            int i = 1;
            foreach (ToolStripMenuItem item in menuItem.DropDownItems)
            {
                item.Text = "&" + i + " " + item.Text;
                i++;
            }
        }

        public static void UpdateMRUMenu(ToolStripMenuItem menuItem)
        {
            Collection<string> ItemsToRemove = new Collection<string>();
            menuItem.DropDownItems.Clear();
            foreach (String mruItem in MRU.Items)
            {
                if (File.Exists(mruItem))
                {
                    FileInfo fi = new FileInfo(mruItem);
                    ToolStripMenuItem itm = new ToolStripMenuItem();
                    itm.Text = fi.Name;
                    itm.Tag = mruItem;
                    itm.Click += new EventHandler(itm_Click);
                    menuItem.DropDownItems.Add(itm);
                }
                else
                {
                    ItemsToRemove.Add(mruItem);
                }
            }
            foreach (String itemToRemove in ItemsToRemove)
            {
                Console.WriteLine("Removing item...");
                Items.Remove(itemToRemove);
            }
            UpdateMRUNumbers(menuItem);
        }

        static void itm_Click(object sender, EventArgs e)
        {
            var menuItem = (ToolStripMenuItem)sender;
            var tag = (string)menuItem.Tag;
            if (ItemClicked != null)
                ItemClicked(null, new MRUEventArgs(tag));
        }
    }
}
